package com.apexedu.eas.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Created with IntelliJ IDEA.
 * User: jeff
 * Date: 15-6-29
 * Time: 下午7:26
 */
public class EasUtil {

    //对比周次 例如：“01-03,05-12,14-16” 和 "01-04,06-07" 应该返回true
    public boolean compareWeekly(String week1, String week2) {
        String week1_all[] = week1.split(",");//{ "01-03", "05-12", "14-16" }
        String week2_all[] = week2.split(",");//{ "01-04", "06-07" }

        //默认不冲突
        boolean flag = false;

        for (int i = 0; i < week1_all.length; i++) {
            String temp_i[] = week1_all[i].split("-");//{"01","03"}
            for (int k = 0; k < week2_all.length; k++) {
                String temp_k[] = week2_all[k].split("-");//{"01","04"}
                int i_0 = Integer.parseInt(temp_i[0]);//01
                int i_1 = Integer.parseInt(temp_i[1]);//03

                int k_0 = Integer.parseInt(temp_k[0]);//01
                int k_1 = Integer.parseInt(temp_k[1]);//04
                //判断两个周次是否冲突
                if (i_1 >= k_0 && i_1 <= k_1 || i_0 >= k_0 && i_0 <= k_1|| i_0 <= k_0 && i_1 >= k_1 || i_0 >= k_0 && i_1 <= k_1) {
                    flag = true;
                    break;
                }
            }
            if(flag){
                break;
            }
        }
        return flag;
    }

    /**
     * 解析properties 配置文件
     * @param proName
     * @return
     */
    public static Properties getPro(String proName){
        try{
            ClassLoader classLoader = EasUtil.class.getClassLoader();
            InputStream is = classLoader.getResourceAsStream(proName);
            Properties props = new Properties();
            props.load(is);
            return props;
        }catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("加载"+proName+"文件错误");
        }
    }

}
